<template>
  <div class="fg1 wf y mt10px" ref="divheight" style="overflow: hidden;">
    <h2 v-if="data.type" v-text="labels[data.type]" class="m10px" style="align-self:center" />
    <div class="wf">
      <el-form label-width="80px" class="x jc">
        <el-form-item label="单据时间">
          <el-date-picker v-model="data.CreateTime" type="datetime" placeholder="选择日期" disabled />
        </el-form-item>
        <el-form-item label="制单人">
          <el-input :value="data.user" disabled />
        </el-form-item>
        <el-form-item label="单据类型">
          <el-input :value="labels[data.type]" disabled />
        </el-form-item>
      </el-form>
    </div>
    <template v-if="!fun">
      <tableIO :value="tb" :buttons="{}" :fun="0" @change="change" />
    </template>
    <template v-if="fun==1">
      <tableIO :value="tb" :buttons="buttons" :fun="1" @change="change" />
    </template>
    <template v-if="fun==2">
      <tableIO :value="tb" :buttons="buttons" :fun="2" @change="change" />
    </template>
    <template v-if="fun==3">
      <tableIO :value="tb" :buttons="buttons" :fun="3" @change="change" />
    </template>
  </div>
</template>
<script>
// import name from "./name";
import tableIO from "./tableIO";
export default {
  components: { tableIO },
  props: ["fun", "data"],
  watch: {
    "$gd.depository"(a) {
      this.tree.depository = a.arr2tree();
    },
    "$gd.project"(a) {
      this.tree.project = a.arr2tree();
    },
    data(a) {
      console.log(123);
      this.tb = a.info;
    }
  },
  data() {
    return {
      labels: ["查看单据", "入库单", "出库单", "库存调整单"],
      buttons: {
        left: [
          {
            id: 1,
            name: "新增一行",
            Attributes: { size: "mini", type: "success" }
          }
        ],
        right: [
          {
            id: 2,
            name: "清空",
            Attributes: { size: "mini", type: "warning" }
          },
          {
            id: 3,
            name: "提交",
            Attributes: { size: "mini", type: "danger" }
          },
          {
            id: 4,
            name: "退出",
            Attributes: { size: "mini", type: "info" }
          }
        ]
      },
      defItems: {
        id: 0,
        uid: "",
        name: "",
        model: "",
        sum: 0,
        depository: "",
        project: [],
        remarks: ""
      },
      tree: {
        userType: [],
        depository: [],
        project: []
      },
      tb: [],
      cut: []
    };
  },
  computed: {},
  mounted() {
    this.$nextTick(() => {
      // 113
      let h = parseInt(window.getComputedStyle(this.$refs.divheight).height);
      // console.log(h-113);
    });
  },
  created() {
    this.mc();
    // divheight
  },
  methods: {
    mc() {
      if (this.data) this.tb = this.data.info;
      this.tree.depository = this.$gd.depository.arr2tree();
      this.tree.project = this.$gd.project.arr2tree();
    },
    send() {
      if (this.tb.length < 1) {
        this.$message.error("请填写数据后再提交");
        return;
      }
      try {
        let tmp = [];
        const data = {
          cmd: 2,
          val: {
            token: sessionStorage.token,
            table: "itemIO",
            type: this.fun,
            items: this.$cp(this.tb).map((a, b) => {
              if (!a.uid || !a.id)
                throw "第" +
                  (b + 1) +
                  "行 [物料编号] 不能为空!且输入后进行选择";
              if (!a.sum) throw "第" + (b + 1) + "行 [数量] 不能为空!";
              if (!a.process) throw "第" + (b + 1) + "行 [工序] 不能为空!";
              if (!a.depository) throw "第" + (b + 1) + "行 [仓库] 不能为空!";
              if (!a.project.length) {
                throw "第" + (b + 1) + "行 [列次] 不能为空!";
              } else {
                a.project = a.project[a.project.length - 1];
                let st = this.$gd.stock.getItem({
                  depository: a.depository,
                  project: a.project,
                  item: a.id,
                  process: a.process
                });
                let t = 0;
                if (st) t = st.stock;
                st = t;
                if (t + a.sum < 0) {
                  t = "第" + (b + 1) + "行 物料[" + a.name + "] ";
                  t +=
                    "仓库[" +
                    this.$gd.depository.getItem(a.depository).name +
                    "] ";
                  t += "项目[" + a.projectName + "] ";
                  t +=
                    "车列[" + this.$gd.project.getItem(a.project).name + "] ";
                  t +=
                    "库存不足[" + Math.abs(a.sum) + "].当前库存为[" + st + "]";
                  throw t;
                }
              } // 判断数量
              if (Array.isArray(a.project))
                a.project = a.project[a.project.length - 1];
              if (
                !this.$gd.stock.getItem({
                  depository: 0,
                  project: a.project,
                  item: a.id,
                  process: a.process
                })
              )
                throw "第" + (b + 1) + "行 [列次][工序][物料] 不匹配!";
              return a;
            })
          }
        };
        // console.log(data);
        // return;
        this.tb = [];
        this.$post(
          data,
          res => {
            // console.log(res);
            if (res.result) {
              this.$formatGD(res.result);
              this.$message.success("数据已提交");
            } else {
              this.$message.error(JSON.stringify(res));
            }
            // this.tb = [];
          },
          false,
          true
        );
      } catch (error) {
        this.$message.error(error);
      }
    },
    change(a, b, c) {
      let t = [];
      switch (a) {
        case 1: // 新增一行
          t = [];
          t.push(this.$cp(this.defItems));
          if (this.data.type == 3) t.push(this.$cp(this.defItems));
          this.tb = this.tb.concat(t);
          break;
        case 2: // 清空
          this.tb = [];
          break;
        case 3: // 提交
          this.send();
          break;
        case 4: // 退出
          this.$router.back();
          break;
        case "sum":
          this.proving(b, c);
          break;
        case "change":
          this.tb = b;
          break;
        default:
          console.log(a, b);
          break;
      }
      // console.log(this.$cp(this.form));
    },
    inDep(a, b) {
      // console.log(a,b);
      if (Math.abs(a) > b) {
        a = 0;
        if (this.data.type) this.$message.error("输入数量超出所需.请更正!");
      }
      return a;
    },
    outDep(a, b) {
      let item = this.$cp(this.tb[a]);
      if (Array.isArray(item.project))
        item.project = item.project[item.project.length - 1];
      if (!item.depository) {
        this.$message.error("输入数量之前,请先选择仓库!");
        return 0;
      }
      b = Math.abs(b);
      item = this.$gd.stock.getItem({
        depository: item.depository,
        project: item.project,
        item: item.id,
        process: item.process
      });
      item = item ? item.stock : 0;
      if (b > Math.abs(item)) {
        this.$message.error(
          "第[" + (a + 1) + "]行.输入数量超出[当前库存].请更正!"
        );
        b = 0;
      }
      return b;
    },
    proving(a, b) {
      if (b == "-") {
        this.tb[a].sum = "-";
        return;
      }
      let t = Number(b);
      console.log(t, b);
      if (b.indexOf(".") == b.length - 1) {
        this.tb[a].sum = t + ".";
        return;
      }
      let tmp = {};
      switch (this.data.type) {
        case 0: // 查询
          console.log("出现逻辑错误");
          break;
        case 1: // 入库单
          t = this.inDep(Math.abs(t), this.tb[a].in);
          break;
        case 2: // 出库单
          t = ~this.outDep(a, t) + 1;
          break;
        case 3: // 库存调整单
          t = Math.abs(t);
          if (a & 1) b = a - 1;
          else {
            b = a + 1;
            t = ~t + 1;
          }
          t = this.outDep(b, t);
          // console.log(this.tb[a]);
          t = this.inDep(t, this.tb[a].in);
          if (
            this.tb[b].project[this.tb[b].project.length - 1] ==
            this.tb[a].project[this.tb[a].project.length - 1]
          ) {
            t = 0;
            this.$message.error("列次不能相同");
          }
          if (
            this.tb[b].process != this.tb[a].process ||
            this.tb[b].id != this.tb[a].id
          ) {
            t = 0;
            this.$message.error("物料工序或物料必须相同");
          }
          this.tb[b].sum = ~t + 1;
          break;

        default:
          // 其他
          break;
      }
      this.tb[a].sum = t ? t : "";
    }
  }
};
</script>

<style lang="less" scoped>
.tst {
  // background-color: rgb(84, 92, 100);
  border: 4px solid #000;
  height: 290px;
}
</style>
